﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 随机生成金币
public class GenerateLevel : MonoBehaviour {


	public GameObject obstacal1;
	public GameObject obstacal2;
	public GameObject obstacal3;
	private GameObject[] posiibleObstacals;
	public static List<Vector2> obstacalsPos = new List<Vector2>();
	public static List<Vector2> mojingPos = new List<Vector2>();
	public static List<Vector2> portalPos = new List<Vector2>();

	public GameObject meadow1;
	public GameObject meadow2;
	public GameObject meadow3;
	public GameObject meadow4;
	public GameObject meadow5;
	public GameObject meadow6;
	private GameObject[] posiiblemeadows;


	public GameObject mojing;


	public GameObject grassBlock1;


	private List<Vector2> posList = new List<Vector2> ();

	private static List<Vector2> superWallPosList = new List<Vector2>();
	// private Dictionary<string, int> typeDic = new Dictionary<string, int>();
	//地图范围
	private int xitm;
	private int yitm;


	// const里当前表项
	private int currentDropExcelIndex;

	



	void Start(){
		
	}

	//创建地图 初始化地图 count：地图中生成可销毁wall的数量
	public void initMap(int count ,int enemyCount, int x , int y ){
		//清空list数据
		superWallPosList.Clear();
		posList.Clear ();

		obstacalsPos.Clear();

		posiibleObstacals= new GameObject[3] { obstacal1, obstacal2, obstacal3 };
		posiiblemeadows = new GameObject[6] { meadow1, meadow2, meadow3, meadow4, meadow5, meadow6 };


		currentDropExcelIndex = 0;


		this.xitm = x;
		this.yitm = y;

		destoryMap ();
		// 生成场景
		//generateTiles();
		initObstaclPos();
		initmojingPos();
		initportalPos();
		//generateObstacles();
		generateMojing();
		//generateGrassBlock();

		
	}


	private void generateGrassBlock() {
		Debug.Log(0 * 60.0f / 100 + 0.3f);
		Instantiate(grassBlock1, new Vector2(0 * 60.0f / 100 + 0.3f, 0 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(0.3f,0.3f), Quaternion.identity);
		/*
		Instantiate(grassBlock1, new Vector2(-9 * 60.0f / 100 + 0.3f, 5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-10 * 60.0f / 100 + 0.3f, 5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-11 * 60.0f / 100 + 0.3f, 5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-12 * 60.0f / 100 + 0.3f, 5 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(-6 * 60.0f / 100 + 0.3f, 4 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(-9 * 60.0f / 100 + 0.3f, 0 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-9 * 60.0f / 100 + 0.3f, 1 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-10 * 60.0f / 100 + 0.3f, 0 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-10 * 60.0f / 100 + 0.3f, 1 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(-6 * 60.0f / 100 + 0.3f, -4 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-7 * 60.0f / 100 + 0.3f, -5 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(-12 * 60.0f / 100 + 0.3f, -5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-13 * 60.0f / 100 + 0.3f, -5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-13 * 60.0f / 100 + 0.3f, -6 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-12 * 60.0f / 100 + 0.3f, -6 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(-11 * 60.0f / 100 + 0.3f, -6 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(2 * 60.0f / 100 + 0.3f, -7 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(5 * 60.0f / 100 + 0.3f, 4 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(5 * 60.0f / 100 + 0.3f, 5 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(6 * 60.0f / 100 + 0.3f, 4 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(6 * 60.0f / 100 + 0.3f, 3 * 60.0f / 100 + 0.3f), Quaternion.identity);

		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, 1 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, 0 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, -1 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, -2 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, -3 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(10 * 60.0f / 100 + 0.3f, -4 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, 3 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, 2 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, 1 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, 0 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, -1 * 60.0f / 100 + 0.3f), Quaternion.identity);
		Instantiate(grassBlock1, new Vector2(11 * 60.0f / 100 + 0.3f, -2 * 60.0f / 100 + 0.3f), Quaternion.identity);
		*/
	}

	private void generateMojing() {

		Instantiate(mojing, new Vector2(0.0f, 0.3f), Quaternion.identity);

	}


	// 横坐标范围 (-16 15)  纵坐标范围(-9 8)
	private void generateTiles() {
		
		for (int i = -Const.CanvasWidthGrid; i < Const.CanvasWidthGrid; i++) {
			for (int j = -Const.CanvasHeightGrid; j < Const.CanvasHeightGrid; j++) {
				int tempInt = Random.Range(0, 6);
				Instantiate(posiiblemeadows[tempInt], new Vector2(i * 60.0f / 100+0.3f, j * 60.0f / 100+0.3f), Quaternion.identity);
			}
		}
	}

	private void initObstaclPos()
	{
		obstacalsPos.Add(new Vector2(-15,-8));
		obstacalsPos.Add(new Vector2(-15, -7));
		obstacalsPos.Add(new Vector2(-15, -6));
		obstacalsPos.Add(new Vector2(-15, -5));
		obstacalsPos.Add(new Vector2(-15, -4));
		obstacalsPos.Add(new Vector2(-15, -2));
		obstacalsPos.Add(new Vector2(-15, -1));
		obstacalsPos.Add(new Vector2(-15, 0));
		obstacalsPos.Add(new Vector2(-15, 1));
		obstacalsPos.Add(new Vector2(-15, 3));
		obstacalsPos.Add(new Vector2(-15, 4));
		obstacalsPos.Add(new Vector2(-15, 5));
		obstacalsPos.Add(new Vector2(-15, 6));
		obstacalsPos.Add(new Vector2(-15, 7));
		obstacalsPos.Add(new Vector2(-14, -8));
		obstacalsPos.Add(new Vector2(-13, -8));
		obstacalsPos.Add(new Vector2(-12, -8));
		obstacalsPos.Add(new Vector2(-11, -8));
		obstacalsPos.Add(new Vector2(-10, -8));
		obstacalsPos.Add(new Vector2(-9, -8));
		obstacalsPos.Add(new Vector2(-8, -8));
		obstacalsPos.Add(new Vector2(-7, -8));
		obstacalsPos.Add(new Vector2(-6, -8));
		obstacalsPos.Add(new Vector2(-5, -8));
		obstacalsPos.Add(new Vector2(-4, -8));
		obstacalsPos.Add(new Vector2(-3, -8));
		obstacalsPos.Add(new Vector2(-2, -8));
		obstacalsPos.Add(new Vector2(0, -8));
		obstacalsPos.Add(new Vector2(1, -8));
		obstacalsPos.Add(new Vector2(2, -8));
		obstacalsPos.Add(new Vector2(3, -8));
		obstacalsPos.Add(new Vector2(4, -8));
		obstacalsPos.Add(new Vector2(5, -8));
		obstacalsPos.Add(new Vector2(6, -8));
		obstacalsPos.Add(new Vector2(7, -8));
		obstacalsPos.Add(new Vector2(8, -8));
		obstacalsPos.Add(new Vector2(9, -8));
		obstacalsPos.Add(new Vector2(10, -8));
		obstacalsPos.Add(new Vector2(11, -8));
		obstacalsPos.Add(new Vector2(12, -8));
		obstacalsPos.Add(new Vector2(13, -8));
		obstacalsPos.Add(new Vector2(14, -8));
		obstacalsPos.Add(new Vector2(14, -7));
		obstacalsPos.Add(new Vector2(14, -6));
		obstacalsPos.Add(new Vector2(14, -5));
		obstacalsPos.Add(new Vector2(14, -3));
		obstacalsPos.Add(new Vector2(14, -2));
		obstacalsPos.Add(new Vector2(14, -1));
		obstacalsPos.Add(new Vector2(14, 1));
		obstacalsPos.Add(new Vector2(14, 2));
		obstacalsPos.Add(new Vector2(14, 3));
		obstacalsPos.Add(new Vector2(14, 5));
		obstacalsPos.Add(new Vector2(14, 6));
		obstacalsPos.Add(new Vector2(13, 7));
		obstacalsPos.Add(new Vector2(12, 7));
		obstacalsPos.Add(new Vector2(11, 7));
		obstacalsPos.Add(new Vector2(10, 7));
		obstacalsPos.Add(new Vector2(9, 7));
		obstacalsPos.Add(new Vector2(8, 7));
		obstacalsPos.Add(new Vector2(7, 7));
		obstacalsPos.Add(new Vector2(6, 7));
		obstacalsPos.Add(new Vector2(5, 7));
		obstacalsPos.Add(new Vector2(4, 7));
		obstacalsPos.Add(new Vector2(3, 7));
		obstacalsPos.Add(new Vector2(2, 7));
		obstacalsPos.Add(new Vector2(1, 7));
		obstacalsPos.Add(new Vector2(-3, 7));
		obstacalsPos.Add(new Vector2(-4, 7));
		obstacalsPos.Add(new Vector2(-5, 7));
		obstacalsPos.Add(new Vector2(-6, 7));
		obstacalsPos.Add(new Vector2(-7, 7));
		obstacalsPos.Add(new Vector2(-8, 7));
		obstacalsPos.Add(new Vector2(-9, 7));
		obstacalsPos.Add(new Vector2(-10, 7));
		obstacalsPos.Add(new Vector2(-11, 7));
		obstacalsPos.Add(new Vector2(-12, 7));
		obstacalsPos.Add(new Vector2(-13, 7));
		obstacalsPos.Add(new Vector2(-14, 7));

		obstacalsPos.Add(new Vector2(-1, -3));
		obstacalsPos.Add(new Vector2(-2, -3));
		obstacalsPos.Add(new Vector2(-3, -3));
		obstacalsPos.Add(new Vector2(0, -3));
		obstacalsPos.Add(new Vector2(1, -3));

		obstacalsPos.Add(new Vector2(-3, 0));
		obstacalsPos.Add(new Vector2(-3, 1));
		obstacalsPos.Add(new Vector2(-3, -1));
		obstacalsPos.Add(new Vector2(-4, -1));
		obstacalsPos.Add(new Vector2(-5, -1));
		obstacalsPos.Add(new Vector2(-5, 1));

		obstacalsPos.Add(new Vector2(1, 0));
		obstacalsPos.Add(new Vector2(1, 1));
		obstacalsPos.Add(new Vector2(1, -1));
		obstacalsPos.Add(new Vector2(2, 1));
		obstacalsPos.Add(new Vector2(3, 1));
		obstacalsPos.Add(new Vector2(3, -1));

		obstacalsPos.Add(new Vector2(-2, 3));
		obstacalsPos.Add(new Vector2(-3, 3));
		obstacalsPos.Add(new Vector2(0, 3));
		obstacalsPos.Add(new Vector2(1, 3));

	}

	private void initmojingPos()
	{
		mojingPos.Add(new Vector2(-1, 0));
		mojingPos.Add(new Vector2(-2, 0));
		mojingPos.Add(new Vector2(0, 0));
		mojingPos.Add(new Vector2(-1, 1));
		mojingPos.Add(new Vector2(-2, 1));
		mojingPos.Add(new Vector2(0, 1));
		mojingPos.Add(new Vector2(-1, -1));
		mojingPos.Add(new Vector2(-2, -1));
		mojingPos.Add(new Vector2(0, -1));


	}


	private void initportalPos()
	{
		portalPos.Add(new Vector2(-1, -4));
		portalPos.Add(new Vector2(14, 7));


	}

	private void generateObstacles() {
		foreach (var tempPos in obstacalsPos) {
			int tempInt = Random.Range(0, 3);
			Instantiate(posiibleObstacals[tempInt], new Vector2(tempPos.x*0.6f+Const.offsetX,tempPos.y*0.6f+Const.offsetY), Quaternion.identity);
		}
	}









    //销毁场景中的物体
    void destoryMap(){
		GameObject[] Grasses = GameObject.FindGameObjectsWithTag ("Grass");	
		GameObject[] Muds = GameObject.FindGameObjectsWithTag ("Mud");	

		foreach (GameObject w in Grasses) {
			Destroy (w);
		}

		foreach (GameObject w in Muds) {
			Destroy (w);
		}

		
	}

}
